//
//  video.c
//  imageToASCII
//
//  Created by Benno Lauther on 16.01.14.
//  Copyright (c) 2014 Benno Lauther. All rights reserved.
//

#include "prototypes.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "typedefs.h"
#include "time.h"
#include <stdarg.h>

void waitFor (unsigned long clocks);

/**
 * runAsciiPixelVideo.
 *
 * <#detailed description#>.
 *
 *  @param  <#parameter#>       <#description#>
 *  @return                  <#description#>
 */
int        runVideo(char* folder, char* format, int frameCount, int optionCount, ... )
{
    char** buffer;
    Size* outputSize= (Size*)malloc(sizeof(Size));
    outputSize->width=80;
    char* fileName = (char*) malloc(strlen(folder)+strlen(format));
    char* currentFileName = (char*) malloc(strlen(format));
    if(outputSize==NULL||fileName==NULL||currentFileName==NULL)
	{
		printf("Allocation failure!");
		return -1;
	}
    enum conversionOption videoMode = noMode;
    enum conversionOption videoLoop = noLoop;
    
    // parse Options
    va_list optionList;
    va_start( optionList, optionCount );
    enum conversionOption currentOption;
    for (int i=0; i<optionCount; i++)
    {
        currentOption = va_arg( optionList, int );
        switch(currentOption)
        {
            case monochromeAscii:
            case coloredPixel:
                videoMode = currentOption;
                break;
            case noLoop:
            case loop:
                videoLoop = currentOption;
                break;
            default:
                return invalidOptionError;
                break;
        }
    }
    
    
    
    buffer = (char**) malloc(frameCount*sizeof(char*));
	if(buffer==NULL)
	{
		printf("Allocation failure!");
		return -1;
	}
    // Buffer All Images
    for (int i=0; i<frameCount; i++)
    {
//        if(!strchr(format,'*'))
//        {
//            return -1;      // TODO Exception handling for no counter(*) in format string
//        }
//        else
//        {
//
//            //TODO @Leander Grund? sprintf(currentFileName,format, i); fand ich eleganter..
//            // Außerdem: Alles was mit Datenverarbeitung zu tun hat zählt normalerweise von 0 an...
//            char* ptrToCounter;
//            char* tmpString;
//            ptrToCounter = strchr(format,'*');
//            int numberCharToCounter = (int)(ptrToCounter - format);
//            tmpString = (char*) malloc(sizeof(char)*numberCharToCounter+1);
//            strncpy(tmpString,format,numberCharToCounter);
//            tmpString[numberCharToCounter] = '\0';
//            sprintf(currentFileName,"%s%d%s",tmpString,i,++ptrToCounter);
//        }
        sprintf(currentFileName,format, i);
        strcpy(fileName, folder);
        strcat(fileName, currentFileName);
		FILE* testFile=fopen(fileName,"r");
		if(testFile==NULL)
		{
			printf("\nInvalid path!\n\n");
			return -2;
		}
        switch(videoMode)
        {
            case coloredPixel:
                buffer[i] = transformImageToAsciiPixel(getBmp(fileName),outputSize);
                break;
            case monochromeAscii:
                buffer[i] = transformImageToAsciiArtMonochrome(getBmp(fileName),outputSize);
                break;
            default:
                return invalidOptionError;
                break;
        }
        
        if(buffer[i]==NULL)
        {
            return missingFrameError;
        }
    }
    
    
    
    
    for(int i=0;i<frameCount;i++)
    {
        waitFor(85000);
        printf("%c[2J",27);
        printf("%s",buffer[i]);
        if(frameCount-1==i && videoLoop == loop)
        {
            i = 0;
            continue;
        }

    }
    return noError;
}

void waitFor (unsigned long clocks) {
    clock_t retClocks = clock() + clocks;     // Get finishing time.
    while (clock() < retClocks);    // Loop until it arrives.
}

